package com.vlong.town.controller.admin.menu;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.menu.Menu;
import com.vlong.town.service.menu.MenuService;
import com.vlong.town.util.common.TownConstant;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController{

	@Autowired
	private MenuService menuService;//菜单业务接口
	/**
	 * 跳转列表页面
	 * @return
	 */
	@RequestMapping("/list")
	public  String  list(){
		return  "admin/menu/list_menu";
	}
	
	/**
	 * 查询菜单
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/treeTable")
	public  DataMessage  treeTable(){
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		List<Menu> resultList = new ArrayList<Menu>();
		List<Menu> menuList = this.menuService.queryList(null, null, null, getOrgId(), null, 0, null, null,
				null, null, null, null, null, null, null, null, null, null);
        for(Menu menu : menuList) {
			if(menu.getMenuLevel().intValue() == 1) {
				this.recursionSubMenu(resultList, menu);
			}
		}
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
		dm.setData(resultList);
		return  dm;
	}
	
	/**
	 * 递归添加菜单子集
	 * @param resultList 结果List
	 * @param parentMenu 父级菜单
	 */
	private void recursionSubMenu(List<Menu> resultList, Menu parentMenu) {
		if(parentMenu.getIsParent().intValue() == 1) {
			List<Menu> subList = this.menuService.querySubList(parentMenu.getMenuId());
			parentMenu.setSubList(subList);
			resultList.add(parentMenu);
			for(Menu sub : subList) {
				if(sub.getIsParent().intValue() == 1) {
					recursionSubMenu(resultList, sub);
				}
			}
		} else {
			resultList.add(parentMenu);
		}
	}
	
	/**
     * 跳转机构添加页面
     * @return String
     */
    @RequestMapping("/addMenu")
    public  String  addMenu(){
    	return  "admin/menu/add_menu";
    }
	
	
    /**
	 * 添加系统菜单根菜单
	 * @param menuName 菜单名称
	 * @param aliasName 菜单别名
	 * @param menuDesc 菜单描述
	 * @param menuUrl 菜单URL
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/executeAddMenu")
	public DataMessage executeAddMenu(
			@RequestParam(required = true, value = "menuName") String menuName,
			@RequestParam(required = false, value = "aliasName") String aliasName,
			@RequestParam(required = false, value = "menuDesc") String menuDesc,
			@RequestParam(required = true, value = "menuUrl") String menuUrl) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(menuName, 1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单名称参数错误");
		} else if(!this.validateStringProperty(aliasName,1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单别名参数错误");
		} else if(!this.validateStringProperty(menuDesc, 1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单说明参数错误");
		} else if(!this.validateStringProperty(menuUrl, 1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单URL参数错误");
		} else {
			boolean isExist = false;
			List<Menu> menuList = this.menuService.queryIsMenuExist(null, menuName, null);
			/*if(menuList != null && menuList.size() > 0) {
				dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单名称已经存在");
				isExist = true;
				return dm;
			} */
			menuList = this.menuService.queryIsMenuExist(null, null, menuUrl);
			if(menuList != null && menuList.size() > 0) {
				dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单URL已经存在");
				isExist = true;
				return dm;
			} 
			if(!isExist) {
				Long menuId = menuService.addRoot(1, getOrgId(), menuName, aliasName, null, menuDesc, menuUrl, null);
				dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
				dm.setData(menuId);
				dm.setMessage("根菜单添加成功");
			}
		}
		return dm;
	}
    
	
	/**
     * 跳转添加跟菜单页面
     * @param menuPid 父级id
     * @return String
     */
    @RequestMapping("/addSubMenu")
    public  String  addSubMenu(@RequestParam(required=false, value="menuPid") String menuPid){
    	request.setAttribute("menuPid", menuPid);
    	return  "admin/menu/add_sub_menu";
    }
    
    
    /**
	 * 添加系统菜单子菜单
	 * @param menuPid 父级Id
	 * @param menuName 菜单名称
	 * @param aliasName 菜单别名
	 * @param menuDesc 菜单描述
	 * @param menuUrl 菜单URL
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/executeAddSubMenu")
	public DataMessage executeAddSubMenu(
			@RequestParam(required = true, value = "menuPid") String menuPid,
			@RequestParam(required = true, value = "menuName") String menuName,
			@RequestParam(required = false, value = "aliasName") String aliasName,
			@RequestParam(required = false, value = "menuDesc") String menuDesc,
			@RequestParam(required = true, value = "menuUrl") String menuUrl) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(menuName, 1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单名称参数错误");
		} else if(!this.validateStringProperty(aliasName,1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单别名参数错误");
		} else if(!this.validateStringProperty(menuDesc, 1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单说明参数错误");
		} else if(!this.validateStringProperty(menuUrl, 1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单URL参数错误");
		} else {
			boolean isExist = false;
			List<Menu> menuList = this.menuService.queryIsMenuExist(null, menuName, null);
			/*if(menuList != null && menuList.size() > 0) {
				dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单名称已经存在");
				isExist = true;
				return dm;
			}*/
			menuList = this.menuService.queryIsMenuExist(null, null, menuUrl);
			if(menuList != null && menuList.size() > 0) {
				dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单URL已经存在");
				isExist = true;
				return dm;
			} 
			if(!isExist) {
				Long menuId = menuService.addSub(Long.parseLong(menuPid), menuName, aliasName, null, menuDesc, menuUrl, null);
				dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
				dm.setData(menuId);
				dm.setMessage("子菜单添加成功");
			}
		}
		return dm;
	}
	
	
	/**
     * 跳转修改菜单页面
     * @param menuId id
     * @return String
     */
    @RequestMapping("/editMenu")
    public  String  editMenu(@RequestParam(required=false, value="menuId") String menuId){
    	Menu menu = menuService.queryById(Long.parseLong(menuId));
    	request.setAttribute("menu", menu);
    	return  "admin/menu/edit_menu";
    }
    
    /**
	 * 编辑系统菜单
	 * @param menuId 菜单Id
	 * @param menuName 菜单名称
	 * @param aliasName 菜单别名
	 * @param menuDesc 菜单描述
	 * @param menuUrl 菜单URL
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/executeEditMenu")
	public DataMessage executeEditMenu(
			@RequestParam(required = true, value = "menuId") String menuId,
			@RequestParam(required = true, value = "menuName") String menuName,
			@RequestParam(required = false, value = "aliasName") String aliasName,
			@RequestParam(required = false, value = "menuDesc") String menuDesc,
			@RequestParam(required = true, value = "menuUrl") String menuUrl) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(menuName, 1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单名称参数错误");
		} else if(!this.validateStringProperty(aliasName,1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单别名参数错误");
		} else if(!this.validateStringProperty(menuDesc, 1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单说明参数错误");
		} else if(!this.validateStringProperty(menuUrl, 1, 50)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单URL参数错误");
		} else {	
			boolean isExist = false;
			List<Menu> menuList = this.menuService.queryIsMenuExist(Long.parseLong(menuId), menuName, null);
			/*if(menuList != null && menuList.size() > 0) {
				dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单名称已经存在");
				isExist = true;
				return dm;
			}*/
			menuList = this.menuService.queryIsMenuExist(Long.parseLong(menuId), null, menuUrl);
			if(menuList != null && menuList.size() > 0) {
				dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
				dm.setMessage("菜单URL已经存在");
				isExist = true;
				return dm;
			} 
			if(!isExist) {
				this.menuService.edit(Long.parseLong(menuId), null,null, null, null, menuName, aliasName, null, menuDesc, menuUrl, null);
				dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
				dm.setMessage("菜单编辑成功");
			}
		}
		return dm;
	}
	
	
	/**
	 * 删除系统菜单
	 * @param menuId 菜单Id
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/delete")
	public DataMessage delete(@RequestParam(required = true, value = "menuId") Long menuId) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(menuId != null && menuId > 0L) {
			menuService.editIsDel(menuId, 1);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("菜单删除成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单Id参数错误");
		}
		return dm;
	}
	
	/**
	 * 批量删除系统菜单
	 * @param menuIds 菜单Ids
	 * @return DataMessage
	 */
	@ResponseBody
	@PostMapping("/batchDelete")
	public DataMessage deleteBatch(@RequestParam(required = true, value = "menuIds") String menuIds) {
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		// 后台数据验证
		if(!this.validateStringProperty(menuIds, 1, 128)) {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("菜单Ids参数错误");
		} else {
			String[] menuIdsArr = menuIds.split(",");
			for(String menuId : menuIdsArr) {
				menuService.editIsDel(Long.parseLong(menuId), 1);
			}
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("菜单删除成功");
		}
		return dm;
	}
	
}
